(function(){
    const fs = require('fs');
    const {resolve, join} = require('path')
    
    class RnlSvgHandler {
      #rootPath = resolve(process.cwd(), 'svg')
      #isRmXml = false
      #handleCount = 0
      constructor(options) {
        this.#isRmXml = options.isRmXml ?? false
      }
      svgFormat(){
        this.#readDirFun((filePath,file) => {
          this.#fillRemoveHandle(filePath,file)
        })
      }
      jsxOrfscComp() {
        console.log('开发中');
      }
      #readDirFun(cb) {
        fs.readdir(this.#rootPath, (err, files) => {
          if(err) throw err
          for(const file of files) {
            const filePath = resolve(this.#rootPath, file)
            if (fs.statSync(filePath).isFile() && file.endsWith('.svg')) {
              cb(filePath,file)
            }
          }
        })
      } 
      #fillRemoveHandle(filePath,file) {
        // console.log(`处理文件：${file}`);
        fs.readFile(filePath,(err, data) => {
          if(err) throw err
          const str = data.toString()
          let newStr = str.replace(/fill="(.*?)"/g, (m, p) => {
            return ''
          })
          if (this.#isRmXml) {
            newStr = newStr.replace(/<\?(.*)\?>/, (m, p) => {
              return ''
            })
          }
          str.replace(/fill="(.*?)"/g, (m, p) => {
            console.log(m, p);
            return ''
          })
          // 异步检查目录是否存在
          const outPath = resolve(process.cwd(), 'svg_dist')
          fs.access(outPath, fs.constants.F_OK, (err) => {
            if (err) {
              fs.mkdirSync(outPath, { recursive: true });
            }
            fs.writeFile(resolve(outPath,file), newStr, (err) => {
              if(err) throw err
              this.#handleCount++
              console.log(`\x1b[36m第${this.#handleCount}个处理完成 -> ${file}\x1b[0m`);
            })
          });
        })
      }

      #svg2jsxfsc() {

      }
    }

    

    const rnlSvgHandle = new RnlSvgHandler({
      isRmXml: true
    })
    rnlSvgHandle.svgFormat() // 格式化svg
    // rnlSvgHandle.jsxOrfscComp() // 生成组件
})()

